package com.cn.login.service.impl;
import com.cn.common.pojo.CommonResult;
import com.cn.common.pojo.LoginParams;
import com.cn.common.pojo.ResultModel;
import com.cn.login.entity.model.Permission;
import com.cn.login.entity.model.User;
import com.cn.login.mapper.UserMapper;
import com.cn.login.service.UserServiceS;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.codec.binary.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashSet;
import java.util.List;

@Service
public class UserServiceSImpl implements UserServiceS{
    @Autowired
    UserMapper userMapper;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    com.cn.login.jwt.JwtTokenUtil jwtTokenUtil;
    @Override
    public User getname(String name) {
        List<User> name1 = userMapper.getname(name);
        Assert.isTrue(name1.size()==1,"你不要瞎几把输名字");
        return (User) name1.get(0);
    }

    @Override
    public List<Permission> getPermissionsByUserId(Integer id) {
        return userMapper.getPermissionByUserId(id);
    }

    @Override
    public String login(LoginParams loginParams) {
        String username = loginParams.getUsername();
        Assert.notNull(username,"账号必须不能为空");
        String password = loginParams.getPassword();
        Assert.notNull(password,"密码必须不能为空");
        // 根据用户查询用户实例
        User getname = getname(username);
        // 验证密码
        String encode = passwordEncoder.encode(password);
        System.out.println(encode);
        boolean matches = passwordEncoder.matches(password, getname.getPassword());
        if(matches){
            // 获取用户所有权限
            List<Permission> permissionsByUserId = getPermissionsByUserId(getname.getId());
            // 将权限list 转化为set形式
            HashSet<Permission> Permissions = new HashSet<>(permissionsByUserId);
            // 将权限设置到用户实例中
            getname.setAuthorities(Permissions);
            // 使用jwtTokenUtil 生成token
            return jwtTokenUtil.generateToken(getname);
        }
        return null;
    }

    @Override
    public CommonResult checkAccessToUri(String uri, String username) {
        if (userMapper.getPermissionByUri(uri)!=null){
        User getname = getname(username);
        List<Permission> permissionsByUserId = getPermissionsByUserId(getname.getId());
        boolean b = permissionsByUserId.stream().anyMatch(Permission -> StringUtils.equals(Permission.getUri(), uri));
        return b? CommonResult.success(true): CommonResult.forbidden(null);}
        return CommonResult.success(true);
    }

    @Override
    public ResultModel delete(int id) {
        int delete = userMapper.delete(id);
        return ResultModel.getResultModelByCount(delete);
    }

    @Override
    public ResultModel update(User user) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = user.getPassword();
        user.setPassword(passwordEncoder.encode(password));
        int update = userMapper.update(user);
        return ResultModel.getResultModelByCount(update);
    }

    @Override
    public ResultModel insert(User user) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = user.getPassword();
        user.setPassword(passwordEncoder.encode(password));
        int insert = userMapper.insert(user);
        return ResultModel.getResultModelByCount(insert);
    }

    @Override
    public ResultModel enable(int id) {
        int enable = userMapper.enable(id);
        return ResultModel.getResultModelByCount(enable);
    }

    @Override
    public ResultModel findById(int id) {
        User byId = userMapper.findById(id);
        return ResultModel.getResultModelByAll(byId,1);
    }

    @Override
    public ResultModel findAll(int limit,int page) {
        PageHelper.startPage(page,limit);
        List<User> all = userMapper.findAll();
        PageInfo<User> PageInfo = new PageInfo<>(all);
        return ResultModel.getResultModel("",0,(int) PageInfo.getTotal(),PageInfo.getList());
    }
}
